/// @Classname : HcAvatorGridGroup
/// @Author :
/// @Time : 2023-04-23 11:15
/// @Description : //todo

import 'package:flutter/material.dart';
import 'package:hc_ui/config/hc_customer_config.dart';
import 'package:hc_ui/theme/hc_avatar_gird_theme.dart';
import 'package:hc_ui/theme/hc_avatar_theme.dart';
import 'package:hc_ui/widget/avatar/hc_avatar.dart';

///头像组
class HcAvatarItem {
  String? backgroundImageUrl;
  String? foregroundImageUrl;
  String? name;
  Color? backgroundColor;
  IconData? icon;
  HcAvatarTheme? themeData;

  HcAvatarItem(
      {this.backgroundColor,
      this.foregroundImageUrl,
      this.name,
      this.icon,
      this.backgroundImageUrl,
      this.themeData});
}

class HcAvatarGridGroup extends StatelessWidget {
  //图片组
  final List<HcAvatarItem> avatarList;

  //覆盖图片
  final String? coverImage;

  //图片组大小
  final double? size;
  final double? hSpacing;
  final double? vSpacing;
  final bool reverse;
  final Color? backgroundColor;
  final double? radius;
  final HcAvatarGridTheme? themeData;

  const HcAvatarGridGroup(
      {super.key,
      required this.avatarList,
      this.coverImage,
      this.size,
      this.reverse = true,
      this.hSpacing,
      this.backgroundColor,
      this.radius,
      this.themeData,
      this.vSpacing});

  @override
  Widget build(BuildContext context) {
    HcAvatarGridTheme theme = themeData ?? HcAvatarGridTheme();
    theme = HcCustomerConfig().allTheme.avatarGridTheme.merge(theme.merge(
        HcAvatarGridTheme(
            size: size,
            backgroundColor: backgroundColor,
            hSpacing: hSpacing,
            vSpacing: vSpacing)));
    //如果是图片组当图片解决
    if (coverImage != null) {
      return HcAvatar(
        size: theme.size,
        radius: theme.radius,
        backgroundImageUrl: coverImage,
      );
    }
    if (avatarList.length == 1) {
      HcAvatarItem item = avatarList[0];
      return HcAvatar(
        size: theme.size,
        radius: theme.radius,
        backgroundImageUrl: item.backgroundImageUrl,
        foregroundImageUrl: item.foregroundImageUrl,
        name: item.name,
        child: item.icon != null ? Icon(item.icon) : null,
      );
    }
    //分割列表最大显示九个
    List<HcAvatarItem> finalList =
        avatarList.length > 9 ? avatarList.sublist(0, 9) : avatarList;
    //文字方向
    TextDirection textDirection = TextDirection.ltr;
    //显示方向
    VerticalDirection verticalDirection = VerticalDirection.down;
    if (reverse) {
      finalList = finalList.reversed.toList();
      textDirection = TextDirection.rtl;
      verticalDirection = VerticalDirection.up;
    }
    return Container(
      height: theme.size,
      width: theme.size,
      decoration: BoxDecoration(
          color: theme.backgroundColor,
          border: Border.all(
              color: theme.borderColor,
              width: theme.borderWidth,
              style:
                  theme.borderWidth > 0 ? BorderStyle.solid : BorderStyle.none),
          borderRadius: BorderRadius.circular(theme.radius)),
      padding: theme.padding,
      child: Center(
        child: Wrap(
          spacing: theme.hSpacing,
          runSpacing: theme.hSpacing,
          direction: Axis.horizontal,
          textDirection: textDirection,
          verticalDirection: verticalDirection,
          alignment: WrapAlignment.center,
          runAlignment: WrapAlignment.spaceBetween,
          crossAxisAlignment: WrapCrossAlignment.center,
          children: finalList
              .map((item) => HcAvatar(
                    borderWidth: 0,
                    size: finalList.length < 5
                        ? ((theme.size - 3 * theme.hSpacing) / 2)
                        : ((theme.size - 4 * theme.hSpacing) / 3),
                    radius: theme.radius,
                    backgroundImageUrl: item.backgroundImageUrl,
                    foregroundImageUrl: item.foregroundImageUrl,
                    name: item.name,
                    child: item.icon != null ? Icon(item.icon) : null,
                  ))
              .toList(),
        ),
      ),
    );
  }
}
